package com.jy.api.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.api.form.QueryCategoryDataForm;
import com.jy.api.form.SaveCategoryDataForm;
import com.jy.framework.utils.PageUtils;
import com.jy.framework.utils.R;
import com.jy.web.diy.entity.BaseInfoEntity;
import com.jy.web.diy.entity.CategoryEntity;
import com.jy.web.diy.service.BaseInfoService;
import com.jy.web.diy.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义模块数据接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/api/category/")
@Api(tags = "自定义模块数据接口")
public class ApiCategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BaseInfoService baseInfoService;

    
    @PostMapping("list")
    @ApiOperation("分页获取指定分类信息")
    public R getCategoryDataList(@RequestBody QueryCategoryDataForm queryCategoryDataForm) {
        BeanMap beanMap = BeanMap.create(queryCategoryDataForm);
//        包装一层，beanMap不能直接put
        Map<String, Object> conditions = new HashMap<>(beanMap);
        CategoryEntity categoryEntity = categoryService.getOne(new QueryWrapper<CategoryEntity>().lambda().eq(CategoryEntity::getCode, queryCategoryDataForm.getCategory()));
        conditions.put("categoryId", categoryEntity.getId());
//        conditions.put("id",queryCategoryDataForm.getId());
        PageUtils diyDataList = baseInfoService.getCategoryDataList(conditions, true);
        return R.ok().put("data", diyDataList);
    }

    @GetMapping("info/{id}")
    @ApiOperation("详细信息")
    public R categoryDataInfo(@PathVariable String id) {
        Map<String, Object> info = baseInfoService.getCategoryDataInfo(id);
        return R.ok().put("data", info);
    }


    /**
     * @Author: JuiceYan
     * @Description: 数据集里面的字段必须和真是一致，也就是不能少key，不然顺序会乱
     * @param: [user, categoryDataForm]
     * @return: com.jy.framework.utils.R
     * @throws:
     * @Date: 1:44 2018/12/25
     */
    @PostMapping("add")
    @ApiOperation("保存指定分类信息")
    public R addCategoryDataList( @RequestBody SaveCategoryDataForm categoryDataForm) {
        List<Map<String, Object>> categoryDataList = categoryDataForm.getCategoryDataList();
        if (CollectionUtil.isEmpty(categoryDataList)) {
            return R.error("分类数据集不能为空！");
        }
        CategoryEntity categoryEntity = categoryService.getOne(new QueryWrapper<CategoryEntity>().lambda().eq(CategoryEntity::getCode, categoryDataForm.getCategory()));
        boolean isCode = true;
        baseInfoService.saveBatchBaseInfos(categoryDataList, String.valueOf(categoryEntity.getId()), isCode);
        return R.ok();
    }
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        baseInfoService.removeById(id);
        return R.ok();
    }

    @PostMapping("edit")
    @ApiOperation("修改指定分类信息")
    public R edit( @RequestBody Map<String,Object> categoryData) {
        if (CollectionUtil.isEmpty(categoryData)) {
            return R.error("分类数据集不能为空！");
        }
        //        需要保持原来默认配置，edit 只是替换
        Serializable id = (Serializable) categoryData.get("id");
        BaseInfoEntity byId = baseInfoService.getById(id);
        CategoryEntity categoryEntity = categoryService.getById(byId.getCategoryId());
        String uuid = R.genUUID();
//        优化为异步处理 todo
        baseInfoService.saveOnseBaseInfo(categoryData, String.valueOf(categoryEntity.getId()), uuid);
//        BaseInfoEntity oldOne = baseInfoService.getById(id);
//        BaseInfoEntity newOne = baseInfoService.getById(uuid);
//        newOne.setName(oldOne.getName());
//        newOne.setCategoryId(oldOne.getCategoryId());
//        newOne.setOrderNum(oldOne.getOrderNum());
//        newOne.setRemark(oldOne.getRemark());
//        baseInfoService.updateBaseInfo(newOne );
//       保存成功，删除原来数据
        baseInfoService.removeById(id);

        return R.ok().put(R.DATA,uuid);
    }




    /**
     * @Author: JuiceYan
     * @Description: 数据集里面的字段必须和真是一致，也就是不能少key，不然顺序会乱
     * @param: [user, categoryDataForm]
     * @return: com.jy.framework.utils.R
     * @throws:
     * @Date: 1:44 2018/12/25
     */
    @PostMapping("addOne")
    @ApiOperation("保存单个分类信息")
    public R addCategoryDataList(  @RequestBody Map<String,Object> categoryData) {
        if (CollectionUtil.isEmpty(categoryData)) {
            return R.error("分类数据集不能为空！");
        }
        String code = (String) categoryData.get("code");
        CategoryEntity categoryEntity = categoryService.getOne(new QueryWrapper<CategoryEntity>().lambda().eq(CategoryEntity::getCode, code));
        String uuid = R.genUUID();
//        优化为异步处理 todo
        categoryData.put("categoryId",categoryEntity.getId());
        baseInfoService.saveOnseBaseInfo(categoryData, String.valueOf(categoryEntity.getId()), uuid);
        return R.ok().put(R.DATA,uuid);
    }

}
